package com.longjianghuasheng.logistics.business.wallet

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.databinding.FragmentWithdrawBinding
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.showEdtDialog
import com.longjianghuasheng.logistics.ext.toastNormal

class WithdrawFragment : BaseTitleFragment<WithdrawViewModel, FragmentWithdrawBinding>() {

    override fun layoutId(): Int = R.layout.fragment_withdraw
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("提现")
        mDatabind.initListener()

    }

    private fun FragmentWithdrawBinding.initListener() {

        rgPay.setOnCheckedChangeListener { group, checkedId ->
            mViewModel.type = if (checkedId == R.id.rb_wechat) 1 else 2
        }

        btnCommit.setOnClickListener {

            mViewModel.price = etMoney.text.toString()


            if (mViewModel.price.isBlank())
                return@setOnClickListener
            if (mViewModel.price.toFloat() > wallet?.toFloat() ?: 0F) {
                toastNormal("可提现金额不足")
                return@setOnClickListener

            }

            showEdtDialog(
                if (mViewModel.type == 1) "微信" else {
                    "支付宝"
                }, "请输入账号", ""
            ) {

                Log.e("TAG", "initView: $it", null)

                mViewModel.type_name = it
                mViewModel.my_withdraw()
            }
        }
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            navigateUp()
        }
    }

    var wallet: String? = ""

    @SuppressLint("SetTextI18n")
    override fun initData() {
        super.initData()
        arguments?.run {
            wallet = getString("wallet")
            mDatabind.tvTotal.text = "总收益（元）：${getString("zong")}"
            mDatabind.tvWithdrawable.text = "可提现（元）：${getString("wallet")}"
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }
}